﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;

public partial class hospital : System.Web.UI.Page
{
    public string search_type = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.Form.Attributes.Add("onSubmit", "javascript:showWorking()");
        //Master.keywordsContent = "top hospitals, hospitals online,  hospital list, search hospitals, board certified hospitals, top hospitals in United States";
        //Master.descriptionContent = "Findadoc.com offers top hospitals online, best hospitals list finder. You can find a hospital according to your requirements easily.";
        if (Request.QueryString["t"] != null)
        {
            if (Request.QueryString["t"].ToString().ToLower() == "usa")
            {
                ////Master.titleContent = "USA Hospital Search Results";
                ////header_label.InnerText = "USA Hospital Search";
                lbl_state.InnerText = "State";
                search_type = "usa";

            }
            else
            {
                //Master.titleContent = "International Hospital Search Results";
                //header_label.InnerText = "International Hospital Search";
                lbl_state.InnerText = "Country";
                search_type = "int";
            }
        }
        else
        {
            //Master.titleContent = "Hospital Search Results";
            //header_label.InnerText = "Hospital Search";
        }

        Master.TdRight = false;

        Master.TopLoginIMG = false;

        
        if (!Page.IsPostBack)
        {
            BindHospitalData();
 
            BindTopHospitals();
        }



    }

    private void BindTopHospitals()
    {
        try
        {
            var ds = (System.Data.DataTable)Session["Top_Hospitals"];
            Top_Hospital_Repeater.DataSource = ds;
            Top_Hospital_Repeater.DataBind();
        }
        catch 
        {
           
        }
       
    }

   
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string name = "", address = "", country = "", zipcode = "";

        if (txtname.Value.Trim().Replace("Search", "") != "")
        {
            name = txtname.Value.Trim();
        }
        if (txtaddress.Value.Trim() != "")
        {
            address = txtaddress.Value.Trim();
        }

        var s = chk_Country.Items.Cast<ListItem>()
                    .Where(item => item.Selected)
                    .Aggregate("", (current, item) => current + (item.Value + ", "));


        if (txtZipcode.Value.Trim() != "")
        {
            zipcode = txtZipcode.Value.Trim();
        }
        if (Request.QueryString["t"] != null)
        {
            if (Request.QueryString["t"].ToString().ToLower() == "usa")
            {
                Session["usa_hospital_search_name"] = name;
                Session["usa_hospital_search_address"] = address;
                Session["usa_hospital_search_state"] = s.TrimEnd(new[] { ',', ' ' });
                Session["usa_hospital_search_zipcode"] = zipcode;
                Response.Redirect(Session["strHttp"] + "hospital_search.aspx?t=usa");
            }
            else
            {
                Session["int_hospital_search_name"] = name;
                Session["int_hospital_search_address"] = address;
                Session["int_hospital_search_country"] = s.TrimEnd(new[] { ',', ' ' });
                Session["int_hospital_search_zipcode"] = zipcode;
                Response.Redirect(Session["strHttp"] + "hospital_search.aspx?t=int");
            }

        }

    }

    private void BindHospitalData()
    {
        string hospitalId = "";

        if (Request.QueryString["id"] != null)
        {
            
            if (Request.QueryString["id"].ToString() != "")
            {

                hospitalId = Request.QueryString["id"];

            }
        }

        var sqlparam = new SqlParameter[] { new SqlParameter("@id", hospitalId) };
        var ds = CustomRepository.CustumDataSetFunction("USP_GET_HOSPITAL_DETAIL", sqlparam);
        if (ds.Tables[0].Rows.Count > 0)
        {
            lblHeaders.InnerText = ds.Tables[0].Rows[0]["Hospital_Name"].ToString();
            lbl_name.InnerText = ds.Tables[0].Rows[0]["Hospital_Name"].ToString();

          //  Master.keywordsContent = "top hospitals, hospitals online,  hospital list, search hospitals, board certified hospitals, top hospitals in United States";
            Master.descriptionContent = ds.Tables[0].Rows[0]["Hospital_Name"].ToString() + ", " + ds.Tables[0].Rows[0]["About"].ToString();
            Master.titleContent = ds.Tables[0].Rows[0]["Hospital_Name"].ToString() + ", " + ds.Tables[0].Rows[0]["About"].ToString();



            if (ds.Tables[0].Rows[0]["About"].ToString().StartsWith(": "))
            {
                lbl_About.InnerText = ds.Tables[0].Rows[0]["About"].ToString().Substring(2);
            }
            else if (ds.Tables[0].Rows[0]["About"].ToString().StartsWith(":"))
            {
                lbl_About.InnerText = ds.Tables[0].Rows[0]["About"].ToString().Substring(1);
            }
            else
            {
                lbl_About.InnerText = ds.Tables[0].Rows[0]["About"].ToString();
            }
            lbl_Address.InnerText = ds.Tables[0].Rows[0]["Address"].ToString();
            lbl_Country.InnerText = ds.Tables[0].Rows[0]["Country"].ToString();
            lbl_Phone.InnerText = ds.Tables[0].Rows[0]["Phone"].ToString();
            lbl_Fax.InnerText = ds.Tables[0].Rows[0]["Fax"].ToString();
            lbl_Website.InnerHtml = CommonFunction.urlconversion(ds.Tables[0].Rows[0]["Website"].ToString() != "" ? ds.Tables[0].Rows[0]["Website"].ToString() : "NA");
            lbl_Email.InnerHtml = CommonFunction.urlconversion(ds.Tables[0].Rows[0]["Email"].ToString() != "" ? ds.Tables[0].Rows[0]["Email"].ToString() : "NA");




        }
        chk_Country.DataSource = ds.Tables[1];
        chk_Country.DataBind();
    }
}